﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

public partial class MyRichTextBox : RichTextBox
{
    private bool boolMultiLine = true;

    public string strFilename = "";

    public MyRichTextBox()
    {
        InitializeComponent();
    }

    public MyRichTextBox(IContainer container)
    {
        container.Add(this);

        InitializeComponent();
    }

    /// <summary>
    /// 保存
    /// </summary>
    public void SaveDefault()
    {
        if (string.IsNullOrEmpty(strFilename)) { return; }
        FileInfo f = new FileInfo(strFilename);
        if (!f.Directory.Exists)
        {
            f.Directory.Create();
        }

        if (strFilename != "")
        {
            if (System.IO.Path.GetExtension(strFilename).ToLower().Equals(".rtf"))
                this.SaveFile(strFilename);
            else
                this.SaveFile(strFilename, RichTextBoxStreamType.PlainText);
        }
    }

    private List<int> searchResult = new List<int>();

    /// <summary>
    /// //查找
    ///
    /// </summary>
    /// <param name="searchString"></param>
    /// <param name="color"></param>
    public int searchString(string searchString, System.Drawing.Color color)
    {
        searchResult.Clear();

        this.Select(0, this.Text.Length);
        this.SelectionColor = System.Drawing.Color.Black;
        int index;

        //第一次搜索
        //index = this.Text.IndexOf(searchString);
        index = this.Find(searchString, RichTextBoxFinds.MatchCase);

        //这个标志是滚动到第一个搜索项
        int flag = index;

        while (index != -1)
        {
            this.Select(index, searchString.Length);
            this.SelectionColor = color;

            index = this.Find(searchString, index + 1, RichTextBoxFinds.MatchCase);
            //index = this.Text.IndexOf(searchString, index + 1);
            if (index != -1)
            {
                searchResult.Add(index);
            }
        }

        if (flag != -1)
        {
            this.Select(flag, searchString.Length);
        }
        //移动到插入符号
        this.ScrollToCaret();

        if (flag == -1)
        {
            return 0;
        }
        else
        {
            return searchResult.Count + 1;
        }
    }

    public void ScrollPosition()
    {
        if (searchResult.Count > 0)
        {
            //            因为搜索不爽所以 code项目变化 net 得到鼠标位置 鼠标点击之后的位置开始搜索
            //滚动是下一个位置 那么现在先得到鼠标的位置，先滚动到鼠标位置
            while (this.SelectionStart > searchResult[0])
            {
                //this.Select(searchResult[0], 1);
                //this.ScrollToCaret();
                searchResult.RemoveAt(0);
            }
            this.Select(searchResult[0], 1);
            this.ScrollToCaret();
            searchResult.RemoveAt(0);
        }
    }

    /// <summary>
    /// 键盘快捷键
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SearchableRichTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        //*********************************************************
        //Add
        //*********************************************************
        if (e.KeyCode == Keys.Q && e.Modifiers == Keys.Control)
        {
            this.SelectionLength = 0;
            this.SelectedText = DateTime.Now.ToLongDateString();

            e.SuppressKeyPress = true; // don't pass the event down
        }
        else if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control)
        {
            SaveDefault();

            e.SuppressKeyPress = true; // don't pass the event down
        }
        else if (e.KeyCode == Keys.W && e.Modifiers == Keys.Control)
        {
            if (boolMultiLine)
            {
                this.Multiline = true;
                this.RightMargin = this.Width - 50;

                this.WordWrap = true;
                boolMultiLine = !boolMultiLine;
            }
            else
            {
                this.Multiline = true;
                this.RightMargin = 100000;

                this.WordWrap = false;
                boolMultiLine = !boolMultiLine;
            }

            e.SuppressKeyPress = true; // don't pass the event down
        }
        else if (e.KeyCode == Keys.F5)
        {
            RunFile.run(strFilename);
            e.SuppressKeyPress = true; // don't pass the event down
        }

        else if (e.KeyCode == Keys.R && e.Modifiers == Keys.Alt)
        {
            //RunFile.run(strFilename);
            //e.SuppressKeyPress = true; // don't pass the event down
            var a = this;
            int i = a.Text.Length;
            int len = 300;
            Random r = new Random();
            if (i - 300 >= 0)
            {
                int start = r.Next(1, i - len);
                a.Select(start, len);
                a.SelectionColor = Color.Red;
                a.ScrollToCaret();
            }

        }

    }
}